package com.smartleanx.module.hrm.app.empRisk;

import cn.hutool.core.collection.CollUtil;
import com.smartleanx.framework.datapermission.core.annotation.DataPermission;
import com.smartleanx.module.hrm.app.risk.RiskHandle;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.service.employee.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 风险点全链路检出执行器
 *
 * @author tengcj
 * @since 2024/6/6
 */
@Slf4j
@Component
public class EmployeeRiskHandleChainService implements ApplicationContextAware {

    List<EmployeeRiskHandle> handleList = new ArrayList<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, EmployeeRiskHandle> serviceMap = applicationContext.getBeansOfType(EmployeeRiskHandle.class);
        handleList = serviceMap.values().stream().sorted(Comparator.comparing(EmployeeRiskHandle::type)).collect(Collectors.toList());
    }

    @DataPermission(enable = false)
    @Transactional(rollbackFor = Exception.class)
    public void execute(Set<Long> employeeIds) {
        if (CollUtil.isEmpty(employeeIds)) {
            return;
        }
        for (EmployeeRiskHandle riskHandle : handleList) {
            riskHandle.handleEmployeeRisk(employeeIds);
        }
    }

}
